<template>
  <div class="device-form">
    <div class="e-nav-bar">
      <div>
        <van-icon class="e-nav-bar__back" name="arrow-left" size="24" @click="back" />
      </div>
      <div class="e-nav-bar__title e-flex-y-center e-flex-between">
        <div class="e-f-24 e-bold ell">{{!isAdd?'修改':'添加'}}其他物联设备</div>
      </div>
    </div>
    <div style="height:90px;"></div>
    <div class="e-pl-10 e-pr-10">
      <!-- 当前单位 -->
      <div class="e-flex-y-center e-flex-between e-form-item">
        <div>
          <span class="e-color-red">*</span>
          <span class="e-bold e-f-16">当前单位</span>
        </div>
        <div class="e-flex-y-center e-flex-grow e-flex-end e-ml-10" @click="openUnit">
          <div class="e-color-333 e-f-14" :class="orgName?'e-color-333':'e-color-holder'">{{orgName||'请选择'}}</div>
          <van-icon v-if="userInfo.isMuilUnitUser" class="e-ml-5" name="arrow" />
        </div>
      </div>
      <!-- 物联设备名称 -->
      <div class="e-flex-y-center e-flex-between e-form-item">
        <div style="width:120px;">
          <span class="e-color-red">*</span>
          <span class="e-bold e-f-16">物联设备名称</span>
        </div>
        <div class="e-flex-grow e-ml-10">
          <van-field v-model="form.name" placeholder="请输入" clearable />
        </div>
      </div>
      <!-- 主机等型号 -->
      <div class="e-flex-y-center e-flex-between e-form-item">
        <div>
          <span class="e-color-red">*</span>
          <span class="e-bold e-f-16">主机等型号</span>
        </div>
        <div class="e-flex-y-center e-flex-grow e-flex-end" @click="openProtocal">
          <div class="e-f-14" :class="protocolName?'e-color-333':'e-color-holder'">{{protocolName||'请选择'}}</div>
          <van-icon class="e-ml-5" name="arrow" />
        </div>
      </div>
      <!-- 复位方式 -->
      <div class="e-flex-y-center e-flex-between e-form-item">
        <div>
          <span class="e-color-red">*</span>
          <span class="e-bold e-f-16">复位方式</span>
        </div>
        <div class="e-flex-y-center e-flex-grow e-flex-end" @click="openReset">
          <div class="e-f-14 e-color-333">{{form.resetMode==1?'处理后复位':'自复位'}}</div>
          <van-icon class="e-ml-5" name="arrow" />
        </div>
      </div>
      <!-- 物联设备类别 -->
      <div class="e-flex-y-center e-flex-between e-form-item">
        <div>
          <span class="e-color-red">*</span>
          <span class="e-bold e-f-16">物联设备类别</span>
        </div>
        <div class="e-flex-y-center e-flex-grow e-flex-end" @click="openDeviceType">
          <div class="e-f-14" :class="deviceTypeName?'e-color-333':'e-color-holder'">{{deviceTypeName||'请选择'}}</div>
          <van-icon class="e-ml-5" name="arrow" />
        </div>
      </div>
      <!-- 系统类型 -->
      <div class="e-flex-y-center e-flex-between e-form-item">
        <div>
          <span class="e-color-red">*</span>
          <span class="e-bold e-f-16">系统类型</span>
        </div>
        <div class="e-flex-y-center e-flex-grow e-flex-end" @click="openSysType">
          <div class="e-f-14" :class="fireSystemTypeName?'e-color-333':'e-color-holder'">{{fireSystemTypeName||'请选择'}}</div>
          <van-icon class="e-ml-5" name="arrow" />
        </div>
      </div>
      <!-- 动态属性 -->
      <div v-for="obj in sysTypeFormList" :key="obj.id">
        <div v-for="(item) in obj.formItems" :key="item.id" class="e-flex-y-center e-flex-between e-form-item">
          <div>
            <span class="e-bold e-f-16">{{item.name + (item.unit!='' ? `（${item.unit}）` : '') + '：'}}</span>
          </div>
          <div class="e-flex-y-center e-flex-grow">
            <van-field v-model="item.val" type="text" :placeholder="'请输入'+item.name" clearable />
          </div>
          <van-icon v-if="item.code=='deviceId'" class="e-ml-5" name="scan" size="24" @click="scan" />
        </div>
      </div>
      <!-- 消防建筑结构 -->
      <div class="e-flex-y-center e-flex-between e-form-item">
        <div>
          <span class="e-color-red">*</span>
          <span class="e-bold e-f-16">消防建筑结构</span>
        </div>
        <div class="e-flex-y-center e-flex-1 e-flex-end e-ml-10" @click="form.organizationId?showfbsIdPicker = true:$toast('请先选择单位')">
          <div class="e-f-14" :class="buildName?'e-color-333':'e-color-holder'">{{buildName||'请选择'}}</div>
          <van-icon class="e-ml-5" name="arrow" />
        </div>
      </div>
      <!-- 具体位置 -->
      <div class="e-flex-y-center e-flex-between e-form-item">
        <div>
          <span class="e-color-red">*</span>
          <span class="e-bold e-f-16">具体位置</span>
        </div>
        <div class="e-flex-grow e-ml-10">
          <van-field v-model="form.location" placeholder="请输入" clearable />
        </div>
      </div>
      <!-- 厂家 -->
      <div class="e-flex-y-center e-flex-between e-form-item">
        <div>
          <span class="e-color-red">*</span>
          <span class="e-bold e-f-16">厂家</span>
        </div>
        <div class="e-flex-y-center e-flex-1 e-flex-end e-ml-10" @click="openProducer">
          <div class="e-f-14 text-right" :class="producerName?'e-color-333':'e-color-holder'">{{producerName||'请选择'}}</div>
          <van-icon class="e-ml-5" name="arrow" />
        </div>
      </div>
      <!-- 关联设备 -->
      <div class="e-flex-y-center e-flex-between e-form-item">
        <div>
          <span class="e-bold e-f-16">关联设备</span>
        </div>
        <div class="e-flex-y-center e-flex-1 e-flex-end e-ml-10" @click="form.organizationId?fcfPopup=true:$toast('请先选择单位')">
          <div class="e-f-14 text-right" :class="fcfName?'e-color-333':'e-color-holder'">{{fcfName||'请选择'}}</div>
          <van-icon class="e-ml-5" name="arrow" />
        </div>
      </div>
      <!-- 关联视频 -->
      <div class="e-flex-y-center e-flex-between e-form-item">
        <div>
          <span class="e-bold e-f-16">关联视频</span>
        </div>
        <div class="e-flex-y-center e-flex-1 e-flex-end e-ml-10" @click="form.organizationId?videoPopup=true:$toast('请先选择单位')">
          <div class="e-f-14 text-right" :class="videoName?'e-color-333':'e-color-holder'">{{videoName||'请选择'}}</div>
          <van-icon class="e-ml-5" name="arrow" />
        </div>
      </div>
    </div>
    <div style="height:100px;"></div>
    <div class="e-fixed-b e-bg-white e-flex-y-center e-flex-between e-p-15">
      <van-button class="e-flex-grow van-button--theme" :disabled="loading" size="large" @click="save()">保存</van-button>
    </div>

    <!-- 选择单位 -->
    <unit-picker v-if="userInfo.isMuilUnitUser" ref="UnitPicker" :showButton="false" :currentOrgprops="currentOrgprops" @change="onUnitChange"></unit-picker>

    <!-- 复位方式 -->
    <van-popup v-model="resetPopup" position="bottom" :style="{ height: '40%' }">
      <van-picker show-toolbar :columns="resetColumns" @confirm="resetChange" @cancel="resetPopup=false" />
    </van-popup>

    <!-- 主机等型号 -->
    <van-popup v-model="protocolPopup" position="bottom" :style="{ height: '50%' }">
      <van-picker
        show-toolbar
        :columns="protocolColumns"
        value-key="name"
        :default-index="defaultProtocalIndex"
        :visible-item-count="5"
        @confirm="protocolChange"
        @cancel="protocolPopup=false"
      />
    </van-popup>

    <!-- 物联设备类别 -->
    <van-popup v-model="deviceTypePopup" position="bottom" :style="{ height: '50%' }">
      <van-picker
        show-toolbar
        :columns="deviceTypeColumns"
        value-key="name"
        :default-index="defaultDeviceTypeIndex"
        :visible-item-count="5"
        @confirm="deviceTypeChange"
        @cancel="deviceTypePopup=false"
      />
    </van-popup>

    <!-- 系统类型 -->
    <van-popup v-model="sysTypePopup" position="bottom" :style="{ height: '50%' }">
      <van-picker
        show-toolbar
        :columns="sysTypeColumns"
        value-key="name"
        :default-index="defaultSysTypeIndex"
        :visible-item-count="5"
        @confirm="sysTypeChange"
        @cancel="sysTypePopup=false"
      />
    </van-popup>

    <!-- 厂家 -->
    <van-popup v-model="producerPopup" position="bottom" :style="{ height: '50%' }">
      <van-picker
        show-toolbar
        :columns="producerColumns"
        value-key="dictValue"
        :default-index="defaultProducerIndex"
        :visible-item-count="5"
        @confirm="producerChange"
        @cancel="producerPopup=false"
      />
    </van-popup>

    <!-- 消防建筑结构 -->
    <van-popup v-model="showfbsIdPicker" round position="bottom">
      <van-cascader
        :options="options_fbsId"
        active-color="#2471CA"
        :field-names="fieldNames"
        :closeable="false"
        @close="showfbsIdPicker = false"
        @change="buildChange"
        @finish="showfbsIdPicker = false"
      >
        <div slot="title" class="e-flex-y-center">
          <div class="e-flex-grow">选择建筑物</div>
          <div class="e-color-primary e-p-5" @click="showfbsIdPicker=false">确认</div>
        </div>
      </van-cascader>
    </van-popup>

    <!-- 选择视频 -->
    <choose-video ref="chooseVideo" v-show="videoPopup" :orgIds="form.organizationId" @change="videoChange" @close="videoPopup=false" />

    <!-- 选择关联设备 -->
    <choose-fcf ref="chooseFcf" v-show="fcfPopup" :orgIds="form.organizationId" @change="fcfChange" @close="fcfPopup=false" />

    <van-dialog
      v-model="showConfirm"
      show-cancel-button
      cancelButtonText="修改设备信息"
      cancelButtonColor="#999"
      show-confirm-button
      confirmButtonText="查看设备"
      confirmButtonColor="#2471CA"
      @confirm="goDetail"
      @cancel="goEdit"
    >
      <div slot="title" class="e-flex-x-center e-relative e-bold e-pb-20 e-pt-20">
        该设备已存在！
        <div class="e-p-5" style="position:absolute;top:-20px;right:5px;" @click="showConfirm=false">
          <van-icon name="close" size="20" />
        </div>
      </div>
    </van-dialog>

    <van-overlay class="e-flex-xy-center" :show="loading" z-index="9" style="background: none;">
      <van-loading size="40px" color="#333" />
    </van-overlay>
  </div>
</template>

<style lang="scss">
.device-form {
  .van-cascader__title {
    flex: 1;
  }
}
</style>

<script>
import Schema from 'async-validator'
import { mapState } from 'vuex'
import ChooseVideo from './ChooseVideo.vue'
import ChooseFcf from './ChooseFcf.vue'
import UnitPicker from '@/components/UnitPicker'
import {
  saveUpSmoke,
  getFcfDetailInfo,
  getFcfCInfo,
  getAssignFcfP,
  getFireCtlCategoryList,
  getFcfCPVal,
} from '@/api/device'
import { getOrgFbsTree, getDict } from '@/api/common'

let rules = {
  organizationId: { required: true, message: '请选择单位' },
  name: {
    asyncValidator: (rule, value) => {
      return new Promise((resolve, reject) => {
        if (!value) {
          reject('请填写设备名称')
        } else if (value.length > 15) {
          reject('设备名称不能超过15位字符')
        } else {
          resolve()
        }
      })
    },
  },
  protocolType: { required: true, message: '请选择主机等型号' },
  fcFacilitiesCategoryId: { required: true, message: '请选择物联设备类别' },
  fireSystemTypeId: { required: true, message: '请选择系统类型' },
  fireBuildingStructureId: {
    required: true,
    message: '请选择消防建筑结构',
  },
  location: {
    asyncValidator: (rule, value) => {
      return new Promise((resolve, reject) => {
        if (!value) {
          reject('请填写具体位置')
        } else if (value.length > 50) {
          reject('具体位置不能超过50位字符')
        } else {
          resolve()
        }
      })
    },
  },
  producerId: {
    required: true,
    message: '请选择厂家',
  },
}

export default {
  components: { UnitPicker, ChooseVideo, ChooseFcf },

  data() {
    return {
      oldInfo: {},
      firstLoad: true,
      loading: false,
      isAdd: true,
      from: '',
      form: {
        id: 0,
        account: '',
        isTop: 1,
        parentId: 0,
        businessId: '',
        deviceCategoryId: 114,
        fcFacilitiesCategoryId: '',
        fireSystemTypeId: '',
        protocolType: '',
        deviceType: 0,
        organizationId: '',
        resetMode: '0',
        name: '',
        fireBuildingStructureId: '',
        location: '',
        relateVideos: '',
        relateFcfIds: '',
        producerId: '',
      },
      resetColumns: [
        { text: '自复位', value: 0 },
        { text: '处理后复位', value: 1 },
      ],
      orgName: '',
      resetPopup: false,
      videoName: '',
      videoPopup: false,

      // 建筑物
      options_fbsId: [],
      fieldNames: {
        text: 'text',
        value: 'id',
        children: 'nodes',
      },
      showfbsIdPicker: false,
      fbsIdValue: '', //所属建筑物 名称
      currentOrgprops: {},
      buildName: '',

      showConfirm: false,

      // 主机等型号
      protocolName: '',
      protocolPopup: false,
      protocolColumns: [],
      defaultProtocalIndex: 0,

      // 物联设备类别
      deviceTypeName: '',
      deviceTypePopup: false,
      deviceTypeColumns: [],
      defaultDeviceTypeIndex: 0,

      // 系统类型
      fireSystemTypeName: '',
      sysTypePopup: false,
      sysTypeColumns: [],
      defaultSysTypeIndex: 0,
      sysTypeFormList: [], // 物联设备类别属性表单

      // 厂家
      producerName: '',
      producerPopup: false,
      producerColumns: [],
      defaultProducerIndex: 0,

      // 关联设备
      fcfPopup: false,
      fcfName: '',
    }
  },

  computed: {
    ...mapState(['userInfo']),
  },

  watch: {
    'form.organizationId'() {
      if (!this.ignoreOrgChange) {
        this.clearVideo()
        this.clearFcf()
        this.clearBuild()
      }
      this.getOrgFbsTree()
    },
    videoPopup(val) {
      if (val) {
        myVue.showChooseVideo = () => {
          this.videoPopup = false
          this.fcfPopup = false
        }
      } else {
        myVue.showChooseVideo = null
      }
    },
    fcfPopup(val) {
      if (val) {
        myVue.showChooseVideo = () => {
          this.videoPopup = false
          this.fcfPopup = false
        }
      } else {
        myVue.showChooseVideo = null
      }
    },
  },

  created() {
    let { code, data = {} } = this.$route.params
    this.oldInfo = data
    if (code) {
      this.deviceId = code
      this.isAdd = true
      this.setDefaultOrg()
      // 新增
    } else {
      this.isAdd = false
      // 编辑
      this.getDetail(data.recordId)
    }
    this.getDict()
    this.getFcFacilitiesCategoryList()
    window.getQode = (code) => {
      this.sysTypeFormList.forEach((n) => {
        n.formItems.forEach((m) => {
          if (m.code == 'deviceId') {
            m.val = code
          }
        })
      })
    }
  },

  mounted() {},

  methods: {
    // 物联设备详情
    getDetail(recordId) {
      this.ignoreOrgChange = true
      this.loading = true
      getFcfDetailInfo({ recordId })
        .then(({ data }) => {
          for (let k in this.form) {
            if (k == 'relateVideos') {
              if (data.relateVideo && data.relateVideo.length > 0) {
                this.form.relateVideos = data.relateVideo
                  .map((n) => {
                    return n.videoId
                  })
                  .join(',')
                this.videoName = data.relateVideo
                  .map((n) => {
                    return n.videoName
                  })
                  .join(',')
              }
            } else if (k == 'relateFcfIds') {
              if (data.relateFcf && data.relateFcf.length > 0) {
                this.form.relateFcfIds = data.relateFcf
                  .map((n) => {
                    return n.facId
                  })
                  .join(',')
                this.fcfName = data.relateFcf
                  .map((n) => {
                    return n.facName
                  })
                  .join(',')
              }
            } else if (k == 'organizationId') {
              this.form.organizationId = data.orgId
            } else {
              this.form[k] = data[k]
            }
          }
          this.orgName = data.orgName
          this.fireSystemTypeName = data.fireSystemTypeName
          this.deviceTypeName = data.fcFacilitiesCategoryName
          this.buildName = data.fbsName
          this.producerName = data.producerName
          this.protocolName = data.protocolName
          this.$nextTick(() => {
            this.ignoreOrgChange = false
          })
          this.getFcfCInfoList(this.form.fcFacilitiesCategoryId)
          this.loading = false
        })
        .catch((e) => {
          console.log('getFcfDetailInfo', e)
          this.loading = false
        })
    },
    requestSave() {
      let that = this
      if (this.loading) return
      let params = {
        ...this.form,
        account: this.userInfo.accountName,
      }
      let propertyVal = []
      for (let n = 0, len = that.sysTypeFormList.length; n < len; n++) {
        let properties = []
        // 获取物联设备类别表单数据
        for (
          let i = 0, len = that.sysTypeFormList[n].formItems.length;
          i < len;
          i++
        ) {
          properties.push({
            fcFacilitiesPropertyId: that.sysTypeFormList[n].formItems[i].id,
            val: that.sysTypeFormList[n].formItems[i].val,
            code: that.sysTypeFormList[n].formItems[i].code,
          })
        }
        propertyVal.push({
          fcFacilitiesCategoryId: that.sysTypeFormList[n].fcfInfo.id,
          properties: properties,
        })
      }
      params.propertyVal = JSON.stringify(propertyVal)
      this.loading = true
      saveUpSmoke(params)
        .then(({ status, info, data }) => {
          if (status == 200) {
            this.$toast('保存成功')
            this.$router.go(-1)
          } else if (status == 400001) {
            this.showRepeatTip(data)
          } else {
            this.$toast(info)
            this.loading = false
          }
        })
        .catch(() => {
          this.loading = false
        })
    },
    validate() {
      new Schema(rules).validate(this.form, { first: true }, (errors) => {
        if (errors) {
          return this.$toast(errors[0].message)
        }
        this.requestSave()
      })
    },
    save() {
      this.validate()
    },
    goDetail() {
      window.top.goDeviceDetail({
        fcFacilitiesId: this.repeatBizId,
      })
    },
    goEdit() {
      this.clearFormData()
      this.getDetail({
        recordId: this.repeatBizId,
      })
    },
    showRepeatTip(id) {
      this.repeatBizId = id
      this.showConfirm = true
    },
    // 获取设施类别详细信息
    getFcfCInfoList(id) {
      this.sysTypeFormList = []
      let that = this
      getFcfCInfo({
        id: id,
      }).then((res) => {
        if ((res.status = 200)) {
          // 如果是编辑回显，获取填写的数据
          if (that.oldInfo.recordId && that.firstLoad) {
            getFcfCPVal({
              fcfId: Number(that.oldInfo.recordId),
            }).then((res) => {
              if (res.status === 200) {
                that.handlerChildValue(res.data[0])
              }
            })
          } else {
            that.handlerChild(res.data)
          }
          // console.log(this.sysTypeFormList)
          that.firstLoad = false
        }
      })
    },
    // 新增获取类别子表单
    handlerChild(data) {
      let that = this
      getAssignFcfP({
        id: data.id,
        isRelationChild: data.isRelationChild,
      }).then((res) => {
        if (res.status === 200) {
          for (let i = 0, len = res.data.length; i < len; i++) {
            res.data[i]['val'] = ''
            if (res.data[i].code == 'deviceId' && this.isAdd) {
              res.data[i].val = this.deviceId
            }
          }
          that.sysTypeFormList.push({
            fcfInfo: data,
            formItems: res.data,
          })
        }
      })

      if (data.isRelationChild == 1 && data.nodes) {
        for (let i = 0, len = data.nodes.length; i < len; i++) {
          that.handlerChild(data.nodes[i])
        }
      }
    },
    // 编辑获取类别子表单
    handlerChildValue(data) {
      let that = this

      if (data.properties) {
        that.sysTypeFormList.push({
          fcfInfo: data,
          formItems: data.properties,
        })
      }

      if (data.nodes && data.nodes.length > 0) {
        for (let i = 0, len = data.nodes.length; i < len; i++) {
          that.handlerChildValue(data.nodes[i])
        }
      }
    },
    // 获取字典
    getDict() {
      // 主机等型号
      getDict({ code: 81 }).then((res) => {
        this.protocolColumns = res.data.dictValueObj.map((n) => {
          return JSON.parse(n.dictValue)
        })
      })
      // 系统类型
      getDict({ code: 55 }).then((res) => {
        this.sysTypeColumns = res.data.dictValueObj.map((n) => {
          return JSON.parse(n.dictValue)
        })
      })
      // 厂家
      getDict({ code: 2 }).then((res) => {
        this.producerColumns = res.data.dictValueObj
      })
    },
    // 获取物联设备类别
    getFcFacilitiesCategoryList() {
      let that = this
      getFireCtlCategoryList({
        queryType: 0,
        fireSystemId: -1,
      }).then((res) => {
        if (res.status === 200) {
          that.deviceTypeColumns = res.data
        }
      })
    },
    openProtocal() {
      this.defaultProtocalIndex =
        this.protocolColumns.findIndex((n) => {
          return n.systemtype == this.form.protocolType
        }) || 0
      this.protocolPopup = true
    },
    protocolChange(val) {
      this.protocolPopup = false
      this.form.protocolType = val.systemtype
      this.protocolName = val.name
    },
    openDeviceType() {
      this.defaultDeviceTypeIndex =
        this.deviceTypeColumns.findIndex((n) => {
          return n.id == this.form.fcFacilitiesCategoryId
        }) || 0
      this.deviceTypePopup = true
    },
    deviceTypeChange(val) {
      this.deviceTypePopup = false
      this.form.fcFacilitiesCategoryId = val.id
      this.deviceTypeName = val.name
      this.form.fireSystemTypeId = val.fireSystemId.toString()
      this.fireSystemTypeName = val.fireSystemName
      this.getFcfCInfoList(val.id)
    },
    openSysType() {
      this.defaultSysTypeIndex =
        this.sysTypeColumns.findIndex((n) => {
          return n.id == this.form.fireSystemTypeId
        }) || 0
      this.sysTypePopup = true
    },
    sysTypeChange(val) {
      this.sysTypePopup = false
      this.form.fireSystemTypeId = val.id
      this.fireSystemTypeName = val.name
    },
    openProducer() {
      this.defaultProducerIndex =
        this.producerColumns.findIndex((n) => {
          return n.id == this.form.producerId
        }) || 0
      this.producerPopup = true
    },
    producerChange(val) {
      this.producerPopup = false
      this.form.producerId = val.dictKey
      this.producerName = val.dictValue
    },
    openUnit() {
      if (!this.userInfo.isMuilUnitUser) return
      this.$refs.UnitPicker.active = true
    },
    onUnitChange(val) {
      this.form.organizationId = val.organizationId
      this.orgName = val.shortName
      localStorage.setItem('ilink_addDeviceDefaultOrgName', val.shortName)
      localStorage.setItem('ilink_addDeviceDefaultOrgId', val.organizationId)
    },
    resetChange(val) {
      this.form.resetMode = val.value
      this.resetPopup = false
    },
    openReset() {
      this.resetPopup = true
    },
    videoChange(val) {
      this.form.relateVideos = val.value
      this.videoName = val.name
    },
    clearVideo() {
      this.form.relateVideos = ''
      this.videoName = ''
    },
    fcfChange(val) {
      this.form.relateFcfIds = val.value
      this.fcfName = val.name
    },
    clearFcf() {
      this.form.relateFcfIds = ''
      this.fcfName = ''
    },
    buildChange({ selectedOptions }) {
      this.buildName = selectedOptions.map((option) => option.text).join('>')
      this.form.fireBuildingStructureId =
        selectedOptions[selectedOptions.length - 1].id
    },
    clearBuild() {
      this.form.fireBuildingStructureId = ''
      this.buildName = ''
    },
    getOrgFbsTree() {
      if (!this.form.organizationId) return
      getOrgFbsTree({
        orgIds: this.form.organizationId,
        buildId: 0,
      }).then((res) => {
        this.options_fbsId = res.data
      })
    },
    goForm(code) {
      this.$router.replace({
        name: 'DeviceFormOther',
        params: {
          code,
          type: this.type,
        },
      })
    },
    goStart() {
      this.$router.replace({
        name: 'AddDevice',
      })
    },
    scan() {
      this.$native.scanQrCode()
    },
    back() {
      this.$router.go(-1)
    },
    clearFormData() {
      Object.keys(this.form).forEach((n) => {
        this.form[n] = ''
      })
      this.form.resetMode = '0'
      this.videoName = ''
      this.buildName = ''
      this.orgName = ''
      this.fireSystemTypeName = ''
      this.$refs.chooseVideo.videoIds = []
    },
    resetForm() {
      this.clearFormData()
      this.setDefaultOrg()
    },
    setDefaultOrg() {
      if (this.isAdd) {
        if (!this.userInfo.isMuilUnitUser) {
          if (this.userInfo.org) {
            this.orgName = this.userInfo.org.name
            this.form.organizationId = this.userInfo.org.id
          }
        } else {
          this.orgName =
            localStorage.getItem('ilink_addDeviceDefaultOrgName') || ''
          this.form.organizationId =
            localStorage.getItem('ilink_addDeviceDefaultOrgId') || ''
        }
      }
    },
  },
}
</script>